package old;

import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.Paint.Align;
import android.graphics.Paint.Style;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.AbsListView.OnScrollListener;

public class GrAPI_old extends View implements OnScrollListener{

	private boolean isPointTouched = false;
	private boolean isBgPointTouched = false;
	private int k = 0;
	private float scale=1;
	private String[] horLabels=null;
	private String[] verLabels=null;
	private float[] values=null;
	private int lineColor = Color.BLACK;
	private float boundingLimit=100;
	private String title="";
	private boolean redFlag=false;
	private boolean showLimits=false;
	private String type=BAR;
		protected final static String BAR = "BAR";
		protected final static String LINE = "LINE";
	private boolean showNumbersOnBar=false;
	private int barWidth=20/2;
	private int pointRadius=5;
	private String xCaption="";
	private int[] xPoint, xPoint2;
	private int[] yPoint, yPoint2;
	private Context context;
	private int xPadding=75;
	private int yPadding=25;
	private Path coloredPath;
	private boolean missing=false;
	private float taboo=0f;
	private float[] bgValues;
	private boolean isNormalTap;
	
	//ControlPanel buttons
	Rect graphLeftButton, graphRightButton, closePanelButton;
	
	public GrAPI_old(Context context, String[] horLabels, String[] verLabels, float[] values, String title, String type, float taboo) {
		super(context);
		this.context = context;
		this.horLabels=horLabels;
		this.verLabels=verLabels;
		this.values = values;
		this.title = title;
		this.type=type;
		this.taboo = taboo;
		float maxValue=getMax(values);
		scale  = context.getResources().getDisplayMetrics().density;
//		boundingLimit = (float) (maxValue+(maxValue*0.25))*scale;
		boundingLimit=(float) (maxValue+(maxValue*0.25));
		
		xPadding *= scale;
		yPadding *= scale;
		pointRadius*=scale;
		if(Array.getLength(verLabels)!=Array.getLength(values)){
			//Since labels do not match values..
			redFlag=true;
		}
	}
	
	@Override
	protected void onDraw(final Canvas canvas) {
		canvas.setDensity((int) (context.getResources().getDisplayMetrics().density)*100);
		System.out.println(scale);
		if(redFlag==false){
			
			Rect boundingBox = new Rect();
			boundingBox.set(xPadding, yPadding, getWidth(), (int) (getHeight()-yPadding-(15*scale)));
			Paint boundingBoxPaint = new Paint();
			boundingBoxPaint.setColor(lineColor);
			boundingBoxPaint.setStyle(Paint.Style.STROKE);
			boundingBoxPaint.setAntiAlias(true);
			canvas.drawRect(boundingBox, boundingBoxPaint);
			Paint textPaint = new Paint();
			textPaint.setTextSize(15*scale);
			textPaint.setTextAlign(Align.CENTER);
			textPaint.setColor(lineColor);
			textPaint.setAntiAlias(true);
			canvas.drawText(title, getWidth()-(getWidth()/2), yPadding/2,textPaint);
			//Supposing that there are 4 horizontal labels...
	//		int hLabels = 4+1;
			int hLabels = Array.getLength(horLabels)+1;
			
			int hInterval = ((getHeight()-yPadding)-yPadding)/hLabels;
			int x=xPadding-2;
			int y=yPadding+hInterval;
			int[] hInts = new int[hLabels];		//These are the graphical points on the x axis
			Paint dividerPaint = new Paint();
			dividerPaint.setAntiAlias(true);
			dividerPaint.setColor(lineColor);
			for(int i=0;i<hLabels;i++){
				canvas.drawLine(x, y, x+4, y, dividerPaint);
				hInts[i] = y;
				y+=hInterval;
			}
			//Supposing that there are 6 vertical labels...
	//		int vLabels=6+1;
			int vLabels = Array.getLength(verLabels)+1;
			int vInterval = (getWidth()-xPadding)/vLabels;
			int vX = xPadding + vInterval;
			int vY = (int) ((getHeight()-yPadding-(15*scale))-2);
			int[] vInts=new int[vLabels];
			for(int i=0;i<vLabels;i++){
				canvas.drawLine(vX, vY, vX, vY+4, dividerPaint);
				vInts[i]=vX;
				vX+=vInterval;
//				vX+=xPadding;
			}
			//Supposing the first value is somewhere around 80
			//we need to get 80% of the total graph height
			//Supposing we have to draw all 6 of them
			//float[] values = {20,30,40,50,40,30};
			//Suppose that background line graph is called bgValues;
			
			if(bgValues!=null){
				xPoint2 = new int[Array.getLength(bgValues)];
				yPoint2 = new int[Array.getLength(bgValues)];
				coloredPath = new Path();
				for(int i=0;i<Array.getLength(bgValues);i++){
	//				if(bgValues[i]==taboo){
	//					bgValues[i] = (bgValues[i-1]+bgValues[i+1])/2;
	//					missingIndices2.add((float) i);
	//				}
					while(bgValues[i]==taboo){
						i++;
					}
					int firstHeight = (int) ((getHeight()-yPadding-(15*scale))*bgValues[i]/boundingLimit);
					int top = (int) (getHeight()-yPadding-(15*scale)-firstHeight);	//Since first height is negative
					yPoint2[i] = top;
					int left = vInts[i];
					xPoint2[i] = left;
					int bottom = (int) (getHeight()-yPadding-(15*scale));
					if(i==0){
						coloredPath.moveTo(xPoint2[i], bottom);
					}
					if(type.contains(BAR)){
						boundingBoxPaint.setStyle(Paint.Style.FILL_AND_STROKE);
						boundingBoxPaint.setColor(Color.DKGRAY);
						Rect bar = new Rect();
						bar.set(left-barWidth, top, left+barWidth, bottom);
						canvas.drawRect(bar, boundingBoxPaint);
					}
					if(showNumbersOnBar==true /*&& !missingIndices2.contains((float) i)*/){
						DecimalFormat df = new DecimalFormat("#0");
						textPaint.setTextAlign(Align.CENTER);
						canvas.drawText(df.format(bgValues[i]), left, top-textPaint.getTextSize(), textPaint);
					}
	//				if(i>0 && type.contains(LINE)){
//					if(!missingIndices2.contains((float) i)){
						dividerPaint.setAntiAlias(true);
						dividerPaint.setColor(Color.LTGRAY);
	//					canvas.drawCircle(xPoint2[i-1], yPoint2[i-1], pointRadius, dividerPaint);
	//					canvas.drawLine(xPoint2[i-1], yPoint2[i-1], xPoint2[i], yPoint2[i], dividerPaint);
						canvas.drawCircle(xPoint2[i], yPoint2[i], pointRadius, dividerPaint);
//					}
	//				}
					coloredPath.lineTo(xPoint2[i], yPoint2[i]);
				}
				coloredPath.lineTo(xPoint2[Array.getLength(xPoint2)-1], getHeight()-yPadding-(scale*15));
				coloredPath.moveTo(xPoint2[Array.getLength(xPoint2)-1], getHeight()-yPadding-(scale*15));
				coloredPath.lineTo(xPoint2[0], getHeight()-yPadding-(scale*15));
				//coloredPath.moveTo(xPoint[0], getHeight()-yPadding-(scale*15));
				//coloredPath.close();
				
				boundingBoxPaint.setColor(Color.LTGRAY);
				boundingBoxPaint.setAlpha(80);
				boundingBoxPaint.setStyle(Style.FILL_AND_STROKE);
				boundingBoxPaint.setAntiAlias(true);
				canvas.drawPath(coloredPath, boundingBoxPaint);
			}
			
			//TODO complete the above method
			xPoint = new int[Array.getLength(values)];
			yPoint = new int[Array.getLength(values)];
			coloredPath = new Path();
			coloredPath.moveTo(xPadding, getHeight()-yPadding-(scale*15));
			dividerPaint.setColor(Color.BLACK);
			for(int i=0;i<Array.getLength(values);i++){
//				if(values[i]==taboo){
//					values[i] = (values[i-1]+values[i+1])/2;
//					missingIndices.add((float) i);
//				}
				while(values[i]==taboo){
					i++;
				}
				System.out.println(values[i] + "/" + boundingLimit);
				int firstHeight = (int) ((getHeight()-yPadding-(15*scale))*(values[i]/boundingLimit));
				System.out.println(firstHeight);
				int top = (int) (getHeight()-yPadding-(15*scale)-firstHeight);	//Since first height is negative
				yPoint[i] = top;
				int left = vInts[i];
				xPoint[i] = left;
				int bottom = (int) (getHeight()-yPadding-(15*scale));
				if(i==0){
					coloredPath.moveTo(xPoint[i], bottom);
				}
				if(type.contains(BAR)){
					boundingBoxPaint.setStyle(Paint.Style.FILL_AND_STROKE);
					boundingBoxPaint.setColor(Color.DKGRAY);
					Rect bar = new Rect();
					bar.set(left-barWidth, top, left+barWidth, bottom);
					canvas.drawRect(bar, boundingBoxPaint);
				}
				System.out.println(missing);
				if(showNumbersOnBar==true /*&& !missingIndices.contains((float) i)*/){
					DecimalFormat df = new DecimalFormat("#0");
					textPaint.setTextAlign(Align.CENTER);
					canvas.drawText(df.format(values[i]), left, top-textPaint.getTextSize(), textPaint);
				}
				System.out.println(missing);
//				if(!missingIndices.contains((float) i)){
					dividerPaint.setAntiAlias(true);
					canvas.drawCircle(xPoint[i], yPoint[i], pointRadius, dividerPaint);
//				}
				coloredPath.lineTo(xPoint[i], yPoint[i]);
			}
			coloredPath.lineTo(xPoint[Array.getLength(xPoint)-1], getHeight()-yPadding-(scale*15));
			coloredPath.moveTo(xPoint[Array.getLength(xPoint)-1], getHeight()-yPadding-(scale*15));
			coloredPath.lineTo(xPoint[0], getHeight()-yPadding-(scale*15));
			//coloredPath.moveTo(xPoint[0], getHeight()-yPadding-(scale*15));
			//coloredPath.close();
			
			boundingBoxPaint.setColor(Color.DKGRAY);
			boundingBoxPaint.setAlpha(80);
			boundingBoxPaint.setStyle(Style.FILL_AND_STROKE);
			boundingBoxPaint.setAntiAlias(true);
			canvas.drawPath(coloredPath, boundingBoxPaint);
			
			//Preparing to set the labels of y-axis...
			textPaint.setColor(lineColor);
			textPaint.setAntiAlias(true);
			textPaint.setTextAlign(Align.CENTER);
			textPaint.setTextSize(15*scale);
			y=getHeight()-yPadding+10;
			for(int i=0;i<Array.getLength(verLabels);i++){
				x=vInts[i];
				canvas.drawText(verLabels[i], x, y, textPaint);
			}
			//Setting the bounding limits
			textPaint.setTextAlign(Align.RIGHT);
			if(showLimits == true){
				DecimalFormat df = new DecimalFormat("#0");
				canvas.drawText(df.format(boundingLimit), xPadding-(scale*5), yPadding+(textPaint.getTextSize()/2), textPaint);
				canvas.drawText("0", xPadding-(scale*5), getHeight()-yPadding-((textPaint.getTextSize()*scale)/2), textPaint);
			}
			
			for(int i=0;i<Array.getLength(hInts)-1;i++){
				canvas.drawText(horLabels[i], xPadding-(scale*5), hInts[i], textPaint);
			}
			canvas.save();
			textPaint.setTextAlign(Align.CENTER);
			textPaint.setTextSize(15*scale);
			canvas.rotate(270, textPaint.getTextSize(),this.getHeight()/2);
			canvas.drawText(yCaption, textPaint.getTextSize(), getHeight()/2, textPaint);
			canvas.restore();
			canvas.drawText(xCaption,((getWidth()+xPadding)/2),getHeight()-(yPadding/9),textPaint);
		}
		this.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				for(int i=0;i<Array.getLength(xPoint);i++){
					int x=(int) event.getX();
					int y=(int) event.getY();
					//The following method will guarantee approximation of the touch event
					Rect r = new Rect();
					Rect r2 = new Rect();
					int offset = (int) (pointRadius + (10*scale)); 
					/*
					 * Since the screen is being touched by a human finger, the 
					 * touch co-ordinates might not be precise. Hence, the offset variable 
					 * determines the approximate area of touch. Now, this may create trouble
					 * in touching two points which are located side by side. 
					 * The Offset variable also plays an important role in defining the
					 * approximate rectangle of touch.
					 */
					r.set(xPoint[i]-offset, yPoint[i]-offset, xPoint[i]+offset, yPoint[i]+offset);
					if(bgValues!=null){
						r2.set(xPoint2[i]-offset, yPoint2[i]-offset, xPoint2[i]+offset, yPoint2[i]+offset);
					}
					
					if(r.contains(x,y)){
						//TODO some logic here to work when the view is touched!
						isPointTouched=true;
						k=i;
						refreshScreen();
					}
					
					if(r2.contains(x,y)){
						//TODO some logic here to work when the view is touched!
						isBgPointTouched=true;
						k=i;
						refreshScreen();
					}
					
					if(graphLeftButton!=null || graphRightButton!=null || closePanelButton!=null){
						if(graphLeftButton.contains(x,y)){
							refreshScreen();
						}else if(graphRightButton.contains(x,y)){
							
						}else if(closePanelButton.contains(x,y)){
							canvas.restore();
						}
					}
					
				}
				if(!isBgPointTouched || !isPointTouched){
					if(isNormalTap){
						isNormalTap=false;
						refreshScreen();
					}else{
						isNormalTap=true;
						refreshScreen();
						canvas.drawColor(Color.LTGRAY);
					}	
				}
				return false;
			}
		});
		
		if(isPointTouched==true){		//That means that touch event on point has happened
			canvas.save();
			Paint linePaint = new Paint();
			linePaint.setColor(Color.BLACK);
			linePaint.setAntiAlias(true);
			linePaint.setStyle(Style.STROKE);
			linePaint.setStrokeWidth(1);
			linePaint.setShadowLayer(2, -2, 2, Color.BLACK);
			canvas.drawLine(xPadding, yPoint[k], xPoint[k], yPoint[k], linePaint);
			canvas.drawLine(xPoint[k], getHeight()-yPadding, xPoint[k], yPoint[k], linePaint);
			isPointTouched=false;
			canvas.restore();
			Timer t = new Timer();
			TimerTask tt = new TimerTask() {
				
				@Override
				public void run() {
					refreshScreen();
				}
			};
			t.schedule(tt, 2000);
		}else if(isBgPointTouched){
			canvas.save();
			Paint linePaint = new Paint();
			linePaint.setColor(Color.LTGRAY);
			linePaint.setAntiAlias(true);
			linePaint.setStyle(Style.STROKE);
			linePaint.setStrokeWidth(1);
			//linePaint.setShadowLayer(2, -2, 2, Color.BLACK);
			canvas.drawLine(xPadding, yPoint2[k], xPoint2[k], yPoint2[k], linePaint);
			canvas.drawLine(xPoint2[k], getHeight()-yPadding, xPoint2[k], yPoint2[k], linePaint);
			isBgPointTouched=false;
			canvas.restore();
			Timer t = new Timer();
			TimerTask tt = new TimerTask() {
				
				@Override
				public void run() {
					refreshScreen();
				}
			};
			t.schedule(tt, 2000);
		}else if(isNormalTap){
			Paint controlPaint = new Paint();
			controlPaint.setColor(Color.DKGRAY);
			controlPaint.setAlpha(200);
			controlPaint.setAntiAlias(true);
			//controlPaint.setStrokeWidth(5);
			controlPaint.setStyle(Style.FILL);
			int height = (int) (getHeight()*0.25);
			Rect controlPanel = new Rect();
			controlPanel.set(0, getHeight()-height, getWidth(), getHeight());
			
			canvas.save();
			canvas.drawRect(controlPanel, controlPaint);
			//To draw buttons;
			controlPaint.setStyle(Style.STROKE);
			controlPaint.setColor(Color.BLACK);
			
			int left, right, top, bottom;
			int padding, btnWidth;
			padding = (int) (10*scale);
			btnWidth = (int) (45*scale);
			left = 0+padding;
			right=left+btnWidth;
			top=controlPanel.top+padding;
			bottom = controlPanel.bottom-padding;
			Paint buttonLabelPaint = new Paint();
			buttonLabelPaint.setColor(Color.BLACK);
			buttonLabelPaint.setTextAlign(Align.CENTER);
			buttonLabelPaint.setTextSize(20*scale);
			buttonLabelPaint.setAntiAlias(true);
			
			graphLeftButton = new Rect();
			graphLeftButton.set(left,top,right,bottom);
			canvas.drawRect(graphLeftButton, controlPaint);
			canvas.drawText("<", (int) ((left+right)/2), (int) (((graphLeftButton.centerY())+(buttonLabelPaint.getTextSize()/2))), buttonLabelPaint);
			
			graphRightButton = new Rect();
			left = right+padding;
			right=left+btnWidth;
			graphRightButton.set(left, top, right, bottom);
			canvas.drawRect(graphRightButton, controlPaint);
			canvas.drawText(">", (int) ((left+right)/2), (int) (((graphRightButton.centerY())+(buttonLabelPaint.getTextSize()/2))), buttonLabelPaint);
			
			
			
			
			//At the end
			closePanelButton = new Rect();
			left = right+padding;
			right=left+btnWidth;
			closePanelButton.set(left, top, right, bottom);
			canvas.drawRect(closePanelButton, controlPaint);
			canvas.drawText("X", (int) ((left+right)/2), (int) (((closePanelButton.centerY())+(buttonLabelPaint.getTextSize()/2))), buttonLabelPaint);
			
			isNormalTap=false;
			canvas.restore();
		}
		super.onDraw(canvas);
		canvas.scale(scale, scale);
	}
	

	private void refreshScreen(){
		super.postInvalidate();
	}
	
	private float getMax(float[] values){
		float maxValue=values[0];
		for(int i=0;i<Array.getLength(values);i++){
			if(maxValue<values[i])
				maxValue=values[i];
		}
		return maxValue;
	}
	
	public float[] getBgValue() {
		return bgValues;
	}

	public void setBgValue(float[] bgValue) {
		this.bgValues = bgValue;
	}

	public String getxCaption() {
		return xCaption;
	}

	public void setxCaption(String xCaption) {
		this.xCaption = xCaption;
	}

	public String getyCaption() {
		return yCaption;
	}

	public void setyCaption(String yCaption) {
		this.yCaption = yCaption;
	}

	String yCaption="";
	public int getPointRadius() {
		return pointRadius;
	}

	public void setPointRadius(int pointRadius) {

		this.pointRadius = (int) (pointRadius*scale);
	}

	public int getBarWidth() {
		return barWidth*2;
	}

	public void setBarWidth(int barWidth) {
		this.barWidth = barWidth/2;
	}
	
	public boolean isShowNumbersOnBar() {
		return showNumbersOnBar;
	}

	public void setShowNumbersOnBar(boolean showNumbersOnBar) {
		this.showNumbersOnBar = showNumbersOnBar;
	}
	
	public boolean areLimitShown(){
		return showLimits;
	}
	
	public void setShowLimits(boolean showLimits){
		this.showLimits = showLimits;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		System.out.println("Scrolled!");
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}

	



}